<?php
session_start();
// 确保用户已登录
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// 数据库连接
$conn = new mysqli("localhost", "root", "123456", "db");
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 获取用户ID
$user_id = $_SESSION['user_id'];

// 查询用户基本信息
$sql_user = "SELECT * FROM users WHERE id = $user_id";
$result_user = $conn->query($sql_user);
$user = $result_user->fetch_assoc();

// 查询用户发布的动态
$sql_posts = "SELECT * FROM posts WHERE user_id = $user_id ORDER BY created_at DESC";
$result_posts = $conn->query($sql_posts);

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo htmlspecialchars($user['name']); ?>'s 个人主页</title>
</head>
<body>
    <h2><?php echo htmlspecialchars($user['name']); ?>'s 个人主页</h2>
    <p>Email: <?php echo htmlspecialchars($user['email']); ?></p>
    <p>收到的点赞数: <?php echo $user['likes_count']; ?></p>

    <h3>我的动态</h3>
    <?php if ($result_posts->num_rows > 0): ?>
        <ul>
            <?php while ($post = $result_posts->fetch_assoc()): ?>
                <li>
                    <p><?php echo htmlspecialchars($post['content']); ?></p>
                    <?php if ($post['image']): ?>
                        <img src="uploads/<?php echo htmlspecialchars($post['image']); ?>" alt="动态图片">
                    <?php endif; ?>
                </li>
            <?php endwhile; ?>
        </ul>
    <?php else: ?>
        <p>暂无动态</p>
    <?php endif; ?>
</body>
</html>